package com.java.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.java.mapper.RootMapper;
import com.java.po.Root;
import com.java.service.RootService;
import com.java.tool.AjaxJson;

/**
 * Title:RootServiceImpl.java
 * Description:
 * @author:sprogrammer
 * @time:2018年2月22日下午7:40:39
 */
public class RootServiceImpl implements RootService{
	@Autowired
	private RootMapper rootMapper;
	private	AjaxJson ajaxJson;
	public RootServiceImpl(){
		@SuppressWarnings("resource")
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
		rootMapper = (RootMapper) applicationContext.getBean("rootMapper");		
		ajaxJson = new AjaxJson();
	}
	
	public void cleanAjaxJson(){
		ajaxJson.setData("");
		ajaxJson.setTotal(0);
		ajaxJson.setMessage("");
	}
	
	@Override
	public AjaxJson LoginRoot(Root root) throws Exception {
		cleanAjaxJson();
		// TODO 自动生成的方法存根
		if(rootMapper.findRootByName(root)==null){
			ajaxJson.setMessage("管理员不存在或密码错误");
			return ajaxJson;
		}
		Root r= rootMapper.findRootByName(root);
		if(!r.getPassword().equals(r.getPassword())){
			ajaxJson.setMessage("管理员不存在或密码错误");
			return ajaxJson;
		}
		ajaxJson.setData(r);
		ajaxJson.setMessage("管理员登录成功");
		return ajaxJson;
	}

	@Override
	public AjaxJson findRootByName(Root root) throws Exception {
		cleanAjaxJson();
		// TODO 自动生成的方法存根
		Root r =rootMapper.findRootByName(root);
		ajaxJson.setData(r);
		ajaxJson.setMessage("success");
		return ajaxJson;
	}
	
}
